package com.fedoom.frontend.views;

import com.fedoom.frontend.models.*;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
 * Game Mode (or game config) view class
 * for changing game settings
 * Extends the ModalView class I created
 * @author tony
 *
 */
// Version 1.01: added GUI components for selecting config file and
// Doom Legacy program (executable) file.
public class ModeView extends ModalView implements ActionListener{
	/**
	 * 
	 */
	JButton doomLegacyProgramButton;
	JButton iwadButton;
	JButton configButton;		// this refers to the config file Doom Legacy generates 
								// (i.e. not a config file we might generate for saving launcher's user settings).
	
	JTextArea iwadText;
	JTextArea configText;
	JTextArea doomLegacyProgramText;
	private String prevIwadText, prevConfigText, prevProgramText;
	/**
	 * @param owner - the jframe that 'contains' this modal dialog
	 * Version 1.01: added GUI components for selecting config file and
	 * doom legacy program (i.e. doom legacy executable).
	 */
	public ModeView(JFrame owner)
	{
		super(owner);
		setSize(600,200);
		this.fileChooser.setVisible(true);
		// choose IWAD (i.e. sometimes referred to as game WAD file)
		//JButton iwadButton = new JButton("Choose iwad File...");
		iwadButton = new JButton("Choose iwad File...");
		iwadButton.addActionListener(this);
		iwadText = new JTextArea();
//		iwadText.setEditable(false);
		iwadText.setLineWrap(true);
		
		// choose config file
		//JButton configButton = new JButton("Choose config file...");
		configButton = new JButton("Choose config file...");
		configButton.addActionListener(this);
		configText = new JTextArea();
//		configText.setEditable(false);
		configText.setLineWrap(true);
		
		// choose Doom Legacy program (i.e. the Legacy executable such as llxdoom).
		doomLegacyProgramButton = new JButton("Choose Doom Legacy program");
		doomLegacyProgramButton.addActionListener(this);
		doomLegacyProgramText = new JTextArea();
//		doomLegacyProgramText.setEditable(false);
		doomLegacyProgramText.setLineWrap(true);

		this.setCommandFieldNames();
		this.addLabelComponentPair(iwadButton, iwadText);
		this.addLabelComponentPair(configButton, configText);
		this.addLabelComponentPair(doomLegacyProgramButton, doomLegacyProgramText);
	}
	/**
	 * method for setting names for command fields
	 */
	private void setCommandFieldNames()
	{
		//these names can be used to know what component or field is selected
		iwadText.setName("2");
		configText.setName("6");		
		
		//what is dlprogram?  what's the command line equivalent (is it this the executable 
		//that's defaulted to /.llxdoom?
		//TM-yes, this is for the command path.  Matt added this so user can specify location of 
		//doom legacy separate of location of Launcher
		//
		doomLegacyProgramText.setName("0");
	}
	/**
	 * constructor - avoid using this constructor
	 */
	public ModeView(){
		this(null);
	}
	
	@Override
	/**
	 * 
	 * Version 1.01: added 'actions' for configButton and doomLegacyProgramButton
	 */
	public void actionPerformed(ActionEvent e) {
/*		JDialog jd = new JDialog(this,Dialog.ModalityType.APPLICATION_MODAL);
		jd.add(this.fileChooser);
		jd.setSize(500, 300);
		jd.setVisible(true);
		*/
		if (e.getSource() == iwadButton) {
			int returnVal = this.fileChooser.showOpenDialog(this);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
	            String s = "\""+this.fileChooser.getSelectedFile()+"\"";
	            this.iwadText.setText(s);
			}
		}
		else if (e.getSource() == configButton) {
			int returnVal = this.fileChooser.showOpenDialog(this);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
	            String s = "\""+this.fileChooser.getSelectedFile()+"\"";
	            this.configText.setText(s);
			}
		}
		else if (e.getSource() == doomLegacyProgramButton) {
			int returnVal = this.fileChooser.showOpenDialog(this);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
	            String s = "\""+this.fileChooser.getSelectedFile()+"\"";
	            this.doomLegacyProgramText.setText(s);
			}
		}

	}
	
	/**
	 * setVisible was overridden in order to store the existing values of 
	 * the text areas before they are edited in the dialog
	 */
	public void setVisible(boolean b)
	{
		if (b==true)
		{
			prevIwadText = iwadText.getText();
			prevConfigText = this.configText.getText();
			prevProgramText = this.doomLegacyProgramText.getText();
		}
		super.setVisible(b);
	}
	
	/**
	 * dispose was overridden to prevent the UI from retaining values when 
	 * a user hits the X to exit a window.
	 * I tested closing windows via OK & via the X.
	 * Only the X calls dispose, so this code won't execute when OK is clicked
	 */
	public void dispose()
	{
		if(prevIwadText!=null && iwadText != null)
			if (!prevIwadText.equals(iwadText.getText()))
				iwadText.setText(prevIwadText);
		if(prevConfigText!=null && configText != null)
			if (!prevConfigText.equals(configText.getText()))
				configText.setText(prevConfigText);
		if(prevProgramText!=null && doomLegacyProgramText != null)
			if (!prevProgramText.equals(doomLegacyProgramText.getText()))
				doomLegacyProgramText.setText(prevProgramText);
		
		super.dispose();
	}
}
